﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project2.设计模式
{
    //事件中心
    public class MessageCenter
    {
        private Dictionary<string, List<Action<string>>> _subscribers = new Dictionary<string, List<Action<string>>>();

        //每一个事件类型对应的是一个列表,列表里面是一系列订阅
        public void Subscribe(string eventType, Action<string> callback)
        {
            if (!_subscribers.ContainsKey(eventType))//没有订阅过的才可以订阅
            {
                _subscribers[eventType] = new List<Action<string>>();
            }
            _subscribers[eventType].Add(callback);
        }

        public void Unsubscribe(string eventType, Action<string> callback)
        {
            if (_subscribers.ContainsKey(eventType))
            {
                _subscribers[eventType].Remove(callback);
            }
        }

        public void Publish(string eventType, string message)
        {
            if (_subscribers.ContainsKey(eventType))
            {
                foreach (var callback in _subscribers[eventType])
                {
                    callback(message);
                }
            }
        }
    }

    public class Publisher
    {
        private MessageCenter _messageCenter;

        //构造函数，实例化一个类
        public Publisher(MessageCenter messageCenter)
        {
            _messageCenter = messageCenter;
        }

        public void PublishMessage(string eventType, string message)
        {
            _messageCenter.Publish(eventType, message);//通过事件中心进行发布
        }
    }

    public interface ISubscriber
    {
        void OnMessageReceived(string message);
    }

    public class Subscriber : ISubscriber
    {
        private string _name;

        public Subscriber(string name)
        {
            _name = name;
        }

        public void OnMessageReceived(string message)
        {
            Console.WriteLine($"{_name} received: {message}");
            Console.WriteLine("");
        }
    }

    internal class ProgramDesign2
    {
        private static void MainDesign2(string[] args)
        {
            MessageCenter messageCenter = new MessageCenter();

            //新建订阅者
            Subscriber subscriber1 = new Subscriber("Newer  1");
            Subscriber subscriber2 = new Subscriber("Newer  2");
            Subscriber subscriber3 = new Subscriber("Gamer 1");
            Subscriber subscriber4 = new Subscriber("Gamer 2");
            Subscriber subscriber5 = new Subscriber("GamerNewer 2");

            //只有关注此事件类型的才会收到订阅消息推送，执行对应的动作
            messageCenter.Subscribe("News", subscriber1.OnMessageReceived);
            messageCenter.Subscribe("News", subscriber2.OnMessageReceived);
            messageCenter.Subscribe("Games", subscriber3.OnMessageReceived);
            messageCenter.Subscribe("Games", subscriber4.OnMessageReceived);
            messageCenter.Subscribe("News", subscriber5.OnMessageReceived);
            messageCenter.Subscribe("Games", subscriber5.OnMessageReceived);

            //实例化发布者
            Publisher publisher = new Publisher(messageCenter);
            //发布消息
            publisher.PublishMessage("News", "Breaking news: New technology released!");
            publisher.PublishMessage("Games", "《明末：渊虚之羽》上线!");

            //移除订阅者
            messageCenter.Unsubscribe("News", subscriber2.OnMessageReceived);
            //差评，取消关注
            messageCenter.Unsubscribe("Games", subscriber4.OnMessageReceived);

            publisher.PublishMessage("News", "Another news: New policy announced!");
            publisher.PublishMessage("Games", "《明末：渊虚之羽》紧急发布 1.3 版本补丁，修复奖励错发问题!");
        }
    }
}